package com.control;

import com.dao.UserDao;

import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import com.model.UserModel;

/**
 * @author :wn
 * @program : 项目五登录界面
 * @descript : 登录
 * @create :2020-10-28 23:05
 */
@WebServlet( "/login")
public class UserControl extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String rem="rem";
        String username=new String (req.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
        String userpassword=new String(req.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        ServletContext applliction=req.getServletContext();
        applliction.setAttribute("username",username);
        String rember=req.getParameter("remember");
        try {
            boolean rs=new UserDao().queryMethod(username,userpassword);
            if(rs){
                if(rem.equals(rember)) {
                    Cookie cookie = new Cookie(username, userpassword);
                    cookie.setMaxAge(3*60);
                    resp.addCookie(cookie);
                }
                HttpSession session=req.getSession();
                session.setAttribute("filter",username);
                req.getRequestDispatcher("/Input.jsp").forward(req,resp);

            }else {
                String information="登录失败！！";
                req.setAttribute("information",information);
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }

    }
}
